package com.hsnn.medstgmini.subscribe.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.subscribe.dao.BidSubscribeDetailDao;
import com.hsnn.medstgmini.subscribe.dao.BidSubscribeRecordDao;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeDetail;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeDetailLog;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeRecord;
import com.hsnn.medstgmini.util.Pagination;
/**
 * @category 预约详情DAO 实现类
 * @author 韩守松
 * @date 2015年8月28日
 */
@Repository
public class BidSubscribeDetailDaoImpl extends BaseHibernateDAO implements BidSubscribeDetailDao {
	
	@Autowired
	private BidSubscribeRecordDao bidSubscribeRecordDao;
	/**
	 * @category 预约详情信息保存
	 * @author 韩守松
	 * @date   2015年8月28日
	 * @param  @param bidSubscribeDetail
	 */
   public void save(BidSubscribeDetail bidSubscribeDetail) {
	   bidSubscribeDetail.setBidSubscribeDetailId(UUID.randomUUID().toString());
	   getSession().saveOrUpdate(bidSubscribeDetail);
	   getSession().flush();
	   saveLog(bidSubscribeDetail);

	}
   

	/**
	 * @category 生产企业预约信息保存
	 * @author 韩守松
	 * @date   2015年9月2日
	 * @param
	 */
	public void proSubscribeSave(BidSubscribeDetail bidSubscribeDetail) {
	  /* bidSubscribeDetail.setBidSubscribeDetailId(UUID.randomUUID().toString());
	   getSession().saveOrUpdate(bidSubscribeDetail);
	   getSession().flush();
	   saveLog(bidSubscribeDetail);*/
	   
	   StringBuffer hql = new StringBuffer();
	   hql.append(" select bid_subscribe_detail_id ");
	   hql.append(" from bid_subscribe_detail  bsd ");
	   hql.append(" where bsd.bid_subscribe_id = :bidSubscribeId ");
	   hql.append(" and bsd.subscribe_date = :subscribeDate ");
	   hql.append(" and bsd.subscribe_starttime = :subscribeStarttime ");
	   hql.append(" and bsd.subscribe_endtime = :subscribeEndtime ");
	   Query query = getSession().createSQLQuery(hql.toString());
	   query.setString("bidSubscribeId", bidSubscribeDetail.getBidSubscribeId());
	   query.setDate("subscribeDate", bidSubscribeDetail.getSubscribeDate());
	   query.setTime("subscribeStarttime",bidSubscribeDetail.getSubscribeStarttime());
	   query.setTime("subscribeEndtime", bidSubscribeDetail.getSubscribeEndtime());
	   
	   String id = query.list().get(0).toString();
	   
	   String bidSubscribeRecordId = bidSubscribeDetail.getRecordId();//预约过的记录id
	   String detailId = bidSubscribeDetail.getDetailId();//预约过的记录所相对应的详情id
	   BidSubscribeRecord bidSubscribeRecord = new BidSubscribeRecord();
	   if(StringUtils.isNotBlank(bidSubscribeRecordId)){
		   bidSubscribeRecord.setBidSubscribeRecordId(bidSubscribeRecordId);
	   }else{
		   bidSubscribeRecord.setBidSubscribeRecordId(UUID.randomUUID().toString());
	   }
	   bidSubscribeRecord.setRecordId(bidSubscribeRecordId);//预约过的记录id
	   bidSubscribeRecord.setDetailId(detailId);//预约过的记录所相对应的详情id
	   bidSubscribeRecord.setBidSubscribeDetailId(id);
	   bidSubscribeRecord.setCompId(bidSubscribeDetail.getUpdUser());
	   bidSubscribeRecord.setProcessStatus(0);
	   bidSubscribeRecord.setUpdDatetime( bidSubscribeDetail.getUpdDatetime());
	   bidSubscribeRecord.setUpdUser(bidSubscribeDetail.getUpdUser());
	   bidSubscribeRecordDao.save(bidSubscribeRecord);
	}
   
   /**
	 * @category 查询预约详情信息
	 * @author 韩守松
	 * @date   2015年8月28日
	 * @param  @param page
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	public Pagination getDetailList(Pagination page,BidSubscribeDetail bidSubscribeDetail) {
		String bidSubscribeId = bidSubscribeDetail.getBidSubscribeId();
		Date subscribeStarttime = bidSubscribeDetail.getSubscribeStarttime();
		//Date subscribeEndtime = bidSubscribeDetail.getSubscribeEndtime();
		Date subscribeDate = bidSubscribeDetail.getSubscribeDate();
		StringBuffer hql = new StringBuffer();
		StringBuffer countHql = new StringBuffer();
		hql.append("from BidSubscribeDetail bsd where 1=1 ");
		countHql.append("select count(*) from from BidSubscribeDetail bsd where 1=1 ");
		if(StringUtils.isNotBlank(bidSubscribeId)){
			hql.append(" and bsd.bidSubscribeId = :bidSubscribeId ");
			countHql.append(" and bsd.bidSubscribeId = :bidSubscribeId ");
		}
		if(null != subscribeStarttime){
			hql.append(" and bsd.subscribeStarttime = :subscribeStarttime ");
			countHql.append(" and bsd.subscribeStarttime = :subscribeStarttime ");
		}
		if(null != subscribeDate){
			hql.append(" and bsd.subscribeDate = :subscribeDate ");
			countHql.append(" and bsd.subscribeDate = :subscribeDate ");
		}
		hql.append(" order by bsd.updDatetime desc ");
		Query countQuery = getSession().createQuery(countHql.toString());
		if(StringUtils.isNotBlank(bidSubscribeId)){
			countQuery.setString("bidSubscribeId", bidSubscribeId);
		}
		if(null != subscribeStarttime){
			countQuery.setDate("subscribeStarttime",subscribeStarttime );
		}
		if(null != subscribeDate){
			countQuery.setDate("subscribeDate", subscribeDate);
		}
		page.setRecords(Long.valueOf(countQuery.uniqueResult().toString()));
		Query query = getSession().createQuery(hql.toString());
		if(StringUtils.isNotBlank(bidSubscribeId)){
			query.setString("bidSubscribeId", bidSubscribeId);
		}
		if(null != subscribeStarttime){
			query.setDate("subscribeStarttime",subscribeStarttime );
		}
		if(null != subscribeDate){
			query.setDate("subscribeDate", subscribeDate);
		}
		query.setFirstResult((page.getPage()-1)*page.getMaxResults());
		query.setMaxResults(page.getMaxResults());
		
		page.setRows(query.list());
	return page;
}
   
	/**
	 * @category 根据预约的id 获取预约详情信息 
	 * @author 韩守松
	 * @date   2015年9月1日
	 * @param  @param bidSubscribeId
	 * @param  @return
	 */
	@SuppressWarnings("unchecked")
	public List<BidSubscribeDetail> getBidSubscribeDetailList(String bidSubscribeId) {
		StringBuffer hql = new StringBuffer();
		hql.append(" from BidSubscribeDetail bsd where bsd.bidSubscribeId = :bidSubscribeId ");
		hql.append(" and bsd.subscribeDate is null ");
		hql.append(" order by bsd.subscribeStarttime ");
		Query query = getSession().createQuery(hql.toString());
		query.setString("bidSubscribeId", bidSubscribeId);
		List<BidSubscribeDetail> list = query.list();
		return list;
	}
	
	/**
	 * @category 更新生产企业预约日期
	 * @author 韩守松
	 * @date   2015年9月2日
	 * @param  @param bidSubscribeDetail
	 */
	public void update(BidSubscribeDetail bidSubscribeDetail) {
		  getSession().saveOrUpdate(bidSubscribeDetail);
		  getSession().flush();
		  saveLog(bidSubscribeDetail);
	}
	
	
	/**
	 * @category 根据预约详情id 获取预约详情信息
	 * @author 韩守松
	 * @date   2015年9月2日
	 * @param  @param bidSubscribeDetailId
	 * @param  @return
	 */
	@SuppressWarnings("unchecked")
	public List<BidSubscribeDetail> getDetailListByDetailId(String bidSubscribeDetailId) {
		StringBuffer hql = new StringBuffer();
		hql.append(" from BidSubscribeDetail bsd where bsd.bidSubscribeDetailId = :bidSubscribeDetailId ");
		Query query = getSession().createQuery(hql.toString());
		query.setString("bidSubscribeDetailId", bidSubscribeDetailId);
		List<BidSubscribeDetail> list = query.list();
		return list;
	}
	
	/**
	 * @category 获取预约日期列表
	 * @author 韩守松
	 * @date   2015年9月6日
	 * @param  @return
	 */
	@SuppressWarnings("unchecked")
	public List<BidSubscribeDetail> getSubscribeDateList(String bidSubscribeId) {
		StringBuffer hql = new StringBuffer();
		hql.append(" from  BidSubscribeDetail bsd ");
		hql.append(" where bsd.bidSubscribeId = :bidSubscribeId ");
		hql.append(" group by  bsd.subscribeDate ");
		hql.append(" order by bsd.subscribeDate ");
		Query query = getSession().createQuery(hql.toString());
		query.setString("bidSubscribeId", bidSubscribeId);
		List<BidSubscribeDetail> list = query.list();
		return list;
	}
	
	/**
	 * @category 获取预约时间列表
	 * @author 韩守松
	 * @date   2015年9月6日
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	@SuppressWarnings("unchecked")
	public List<BidSubscribeDetail> getSubscribeTimeList(BidSubscribeDetail bidSubscribeDetail) {
		StringBuffer hql = new StringBuffer();
		hql.append(" from  BidSubscribeDetail bsd ");
		hql.append(" where bsd.bidSubscribeId = :bidSubscribeId ");
		hql.append(" and   DATE_FORMAT(bsd.subscribeDate,'%Y-%m-%d') = :subscribeDate ");
		hql.append(" order by bsd.subscribeStarttime ");
		Query query = getSession().createQuery(hql.toString());
		query.setString("bidSubscribeId", bidSubscribeDetail.getBidSubscribeId());
		query.setDate("subscribeDate", bidSubscribeDetail.getSubscribeDate());
		List<BidSubscribeDetail> list = query.list();
		return list;
	}
	
	/**
	 * @category 根据 设置id 预约日期 预约时间段确定详情id
	 * @author 韩守松
	 * @date   2015年9月7日
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	public String getRecordDetailId(BidSubscribeDetail bidSubscribeDetail){
		StringBuffer hql = new StringBuffer();
		   hql.append(" select bid_subscribe_detail_id ");
		   hql.append(" from bid_subscribe_detail  bsd ");
		   hql.append(" where bsd.bid_subscribe_id = :bidSubscribeId ");
		   hql.append(" and bsd.subscribe_date = :subscribeDate ");
		   hql.append(" and bsd.subscribe_starttime = :subscribeStarttime ");
		   hql.append(" and bsd.subscribe_endtime = :subscribeEndtime ");
		   Query query = getSession().createSQLQuery(hql.toString());
		   query.setString("bidSubscribeId", bidSubscribeDetail.getBidSubscribeId());
		   query.setDate("subscribeDate", bidSubscribeDetail.getSubscribeDate());
		   query.setTime("subscribeStarttime",bidSubscribeDetail.getSubscribeStarttime());
		   query.setTime("subscribeEndtime", bidSubscribeDetail.getSubscribeEndtime());
		   
		   String id = query.list().get(0).toString();
		   return id;
	}
	
    /**
     * @category 根据预约日期获取预约时间
     * @author 韩守松
     * @date   2015年9月21日
     * @param  @param dateStr
     * @param  @param bidSubscribeId
     * @param  @return
     */
   @SuppressWarnings("unchecked")
public List<BidSubscribeDetail>  getSubscribeTimeByDate(Date dateStr,String bidSubscribeId){
	   StringBuffer hql = new StringBuffer();
	   hql.append(" from BidSubscribeDetail bsd ");
	   hql.append(" where ");
	   hql.append("  bsd.bidSubscribeId = :bidSubscribeId ");
	   hql.append(" and DATE_FORMAT(bsd.subscribeDate,'%Y-%m-%d') = :dateStr ");
	   hql.append(" order by bsd.subscribeStarttime ");
	   Query query = getSession().createQuery(hql.toString());
	   query.setString("bidSubscribeId", bidSubscribeId);
	   query.setDate("dateStr", dateStr);
	   List<BidSubscribeDetail> list = query.list();
	   return list;
   }
	
   public void saveLog(BidSubscribeDetail bidSubscribeDetail){
	   BidSubscribeDetailLog bidSubscribeDetailLog = new BidSubscribeDetailLog();
	   bidSubscribeDetailLog.setLogId(UUID.randomUUID().toString());
	   bidSubscribeDetailLog.setBidSubscribeDetailId(bidSubscribeDetail.getBidSubscribeDetailId());
	   bidSubscribeDetailLog.setBidSubscribeId(bidSubscribeDetail.getBidSubscribeId());
	   bidSubscribeDetailLog.setSubscribeDate(bidSubscribeDetail.getSubscribeDate());
	   bidSubscribeDetailLog.setSubscribeStarttime(bidSubscribeDetail.getSubscribeStarttime());
	   bidSubscribeDetailLog.setSubscribeEndtime(bidSubscribeDetail.getSubscribeEndtime());
	   bidSubscribeDetailLog.setStatus(bidSubscribeDetail.getStatus());
	   bidSubscribeDetailLog.setUpdDatetime(bidSubscribeDetail.getUpdDatetime());
	   bidSubscribeDetailLog.setUpdUser(bidSubscribeDetail.getUpdUser());
	   getSession().saveOrUpdate(bidSubscribeDetailLog);
	   getSession().flush();
   }
}
